package com.hliushi.learn.controller;

import com.hliushi.learn.bean.Blogger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ThymeleafController
 * @Description TODO
 * @Author hliushi
 * @Date 2021/7/6 22:35
 * @Version V1.0
 **/
@Controller     // 使用模板的时候需要使用@Controller注解
@RequestMapping("/thymeleaf")
public class ThymeleafController {


    /**
     * 当我们在浏览器中输入localhost:8001/thymeleaf/test400时, 故意输入错误, 找不到对应的方法, 就会跳转到404.html
     *
     * @param
     * @return
     */
    @RequestMapping("/test404")
    public String test404() {
        return "index";
    }

    /**
     * 当我们在浏览器中输入localhost:8088/thymeleaf/test505时, 会抛出异常, 然后会自动跳转到500.html
     *
     * @param
     * @return
     */
    @RequestMapping("/test500")
    public String test500() {
        int i = 1 / 0;
        return "index";
    }

    /**
     * @param
     * @return
     */
    @RequestMapping("/get/blogger")
    public String getBlogger(Model model) {
        Blogger blogger = new Blogger(10001L, "hliushi", "qwer1234");

        model.addAttribute("blogger", blogger);

        return "user/blogger";
    }

    @RequestMapping("/get/list")
    public String getList(Model model) {
        List<Blogger> list = new ArrayList<>();

        Blogger blogger = new Blogger(10001L, "hliushi", "qwer1234");
        Blogger blogger2 = new Blogger(10002L, "zid", "21314");
        Blogger blogger3 = new Blogger(10003L, "pitter", "21314");
        list.add(blogger);
        list.add(blogger2);
        list.add(blogger3);

        model.addAttribute("list", list);
        return "user/list";
    }
}
